<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java迭代器的艺术与哲学</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f9f9f9;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #282c34;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            background: linear-gradient(90deg, rgba(255,255,255,0) 0%, rgba(255,237,160,0.3) 50%, rgba(255,255,255,0) 100%);
            padding: 0 4px;
            border-radius: 2px;
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.8;
            margin: 0.1em 0.15em 0 0;
            color: #6e8efb;
            font-weight: 700;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <header class="hero text-white py-20 md:py-32 px-6">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">Java迭代器的<span class="highlight">艺术与哲学</span></h1>
                <p class="text-xl md:text-2xl max-w-2xl mb-8 opacity-90">从初学者到专家：探索集合遍历的核心原理与最佳实践</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full flex items-center">
                        <i class="fas fa-code mr-2"></i> 编程思想
                    </span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full flex items-center">
                        <i class="fas fa-bug mr-2"></i> 性能优化
                    </span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full flex items-center">
                        <i class="fas fa-lightbulb mr-2"></i> 最佳实践
                    </span>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-6 py-12">
        <!-- Author & Meta -->
        <div class="flex items-center mb-12">
            <div class="w-12 h-12 rounded-full bg-gray-300 flex items-center justify-center mr-4">
                <i class="fas fa-user text-gray-600 text-xl"></i>
            </div>
            <div>
                <h4 class="font-medium">资深Java工程师</h4>
                <p class="text-gray-500 text-sm">更新于 <span id="current-date"></span> · 10分钟阅读</p>
            </div>
        </div>

        <!-- Article Content -->
        <article class="prose prose-lg max-w-none">
            <p class="drop-cap text-gray-700 leading-relaxed">说到迭代器，我真是又爱又恨。记得我刚接触Java集合框架时，对这个`Iterator`接口既好奇又困惑。它看起来挺简单的，但实际用起来，这玩意儿比想象的要复杂不少。</p>

            <!-- Code Block Section -->
            <div class="my-10">
                <div class="code-block p-6 text-sm md:text-base overflow-x-auto">
                    <pre class="text-gray-200"><code>public interface Iterator&lt;E&gt; {
    boolean hasNext(); // 还有下一个吗？
    E next();          // 给我下一个！
    void remove();     // 把这个扔掉！
}</code></pre>
                </div>
                <div class="text-center mt-2 text-gray-500 text-sm">
                    <i class="fas fa-code mr-1"></i> Java Iterator接口 - 简洁但不简单
                </div>
            </div>

            <!-- ArrayList Iterators Section -->
            <section class="my-16">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 font-serif">ArrayList的迭代器实现</h2>
                <p class="mb-6">我花了好几年才真正理解ArrayList的迭代器实现。这个`Itr`内部类看起来挺无辜：</p>
                
                <div class="code-block p-6 text-sm md:text-base overflow-x-auto mb-6">
                    <pre class="text-gray-200"><code>private class Itr implements Iterator&lt;E&gt; {
    private int cursor;       // 下一个元素的位置
    private int lastRet = -1; // 刚返回的元素位置
    
    // 其他方法省略...
}</code></pre>
                </div>
                
                <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6 rounded">
                    <div class="flex">
                        <div class="flex-shrink-0 text-blue-500">
                            <i class="fas fa-exclamation-circle text-xl"></i>
                        </div>
                        <div class="ml-3">
                            <h3 class="text-sm font-medium text-blue-800">并发修改异常</h3>
                            <div class="mt-2 text-sm text-blue-700">
                                <p>这里有个坑，我曾经在一个高并发系统里被它坑惨了。有一次我在迭代中修改了ArrayList，结果程序崩溃，抛出`ConcurrentModificationException`。当时我还纳闷，"我又不是在多线程环境下用它，怎么会出现并发问题？"</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <p>后来才恍然大悟 - 这不是真的"并发"问题，而是迭代器检测到集合被修改后的自我保护机制。</p>
            </section>

            <!-- For-Each Section -->
            <section class="my-16">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 font-serif">for-each的小秘密</h2>
                
                <div class="grid md:grid-cols-2 gap-8 mb-8">
                    <div class="card bg-white p-6 rounded-lg">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                <i class="fas fa-eye text-purple-500"></i>
                            </div>
                            <h3 class="font-bold text-lg">优雅的for-each</h3>
                        </div>
                        <div class="code-block p-4 text-sm overflow-x-auto">
                            <pre class="text-gray-200"><code>// 看起来很美
for (String name : nameList) {
    System.out.println(name);
}</code></pre>
                        </div>
                    </div>
                    
                    <div class="card bg-white p-6 rounded-lg">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                <i class="fas fa-code text-blue-500"></i>
                            </div>
                            <h3 class="font-bold text-lg">背后的真相</h3>
                        </div>
                        <div class="code-block p-4 text-sm overflow-x-auto">
                            <pre class="text-gray-200"><code>// 实际上是这样的
Iterator&lt;String&gt; it = nameList.iterator();
while (it.hasNext()) {
    String name = it.next();
    System.out.println(name);
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <p class="text-gray-700">这个发现让我当时大呼"卧槽"！然后继续默默地在代码里用for-each循环，因为确实好看多了。</p>
            </section>

            <!-- HashSet Section -->
            <section class="my-16">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 font-serif">HashSet的迭代器</h2>
                
                <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-6 rounded">
                    <div class="flex">
                        <div class="flex-shrink-0 text-yellow-500">
                            <i class="fas fa-lightbulb text-xl"></i>
                        </div>
                        <div class="ml-3">
                            <h3 class="text-sm font-medium text-yellow-800">实习生的困惑</h3>
                            <div class="mt-2 text-sm text-yellow-700">
                                <p>我记得有一次在项目中，有个新来的实习生抱怨HashSet的遍历顺序"有问题"。我当时笑了：HashSet压根就不保证顺序，它的迭代器实现大概是这样：</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="code-block p-6 text-sm md:text-base overflow-x-auto mb-6">
                    <pre class="text-gray-200"><code>private class HashIterator implements Iterator&lt;E&gt; {
    Node&lt;K,V&gt; next;
    
    // 省略其他代码...
    
    public E next() {
        // 根据哈希表的结构查找下一个元素
        // 反正不会按你想的顺序来就是了
    }
}</code></pre>
                </div>
                
                <p class="text-gray-700">我告诉他："如果你想要有序的Set，用LinkedHashSet或TreeSet吧！"他一脸恍然大悟。</p>
            </section>

            <!-- Performance Section -->
            <section class="my-16">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 font-serif">性能那些事儿</h2>
                
                <div class="mermaid mb-8">
                    graph TD
                        A[遍历方式选择] --> B[ArrayList]
                        A --> C[LinkedList]
                        A --> D[HashSet/HashMap]
                        B --> B1[索引遍历或迭代器]
                        C --> C1[只用迭代器]
                        D --> D1[不考虑顺序]
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <h3 class="font-bold text-lg mb-4 text-gray-800">高效遍历</h3>
                        <div class="code-block p-4 text-sm overflow-x-auto">
                            <pre class="text-gray-200"><code>// 对ArrayList来说，这样反而更高效
for (int i = 0; i < list.size(); i++) {
    doSomething(list.get(i));
}</code></pre>
                        </div>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <h3 class="font-bold text-lg mb-4 text-gray-800">性能陷阱</h3>
                        <div class="code-block p-4 text-sm overflow-x-auto">
                            <pre class="text-gray-200"><code>// 这段代码对LinkedList是O(n²)的灾难
for (int i = 0; i < linkedList.size(); i++) {
    process(linkedList.get(i));
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="bg-blue-50 p-6 rounded-lg mb-6">
                    <h3 class="font-bold text-lg mb-4 flex items-center">
                        <i class="fas fa-check-circle text-blue-500 mr-2"></i>
                        <span>最佳实践</span>
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span><span class="font-medium">ArrayList:</span> 索引遍历或迭代器都行</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span><span class="font-medium">LinkedList:</span> 只用迭代器，绝不用索引</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span><span class="font-medium">HashSet/HashMap:</span> 别考虑顺序，专注效率</span>
                        </li>
                    </ul>
                </div>
                
                <p class="text-gray-700">对了，Java 8引入的Stream API也挺香的，尤其是并行流：</p>
                
                <div class="code-block p-6 text-sm md:text-base overflow-x-auto my-6">
                    <pre class="text-gray-200"><code>list.parallelStream()
    .filter(item -> item.isValid())
    .forEach(this::process);</code></pre>
                </div>
                
                <p class="text-gray-700">但也别迷信它，我发现在数据量不大时，普通循环反而更快更直观。</p>
            </section>

            <!-- Final Thoughts -->
            <section class="my-16 bg-gray-50 p-8 rounded-lg">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 font-serif text-center">心得体会</h2>
                <div class="max-w-2xl mx-auto text-center">
                    <p class="text-lg mb-6">说了这么多，其实我想表达的是：了解迭代器和集合遍历的底层原理真的很重要。表面上看，遍历集合就是个简单循环，但背后涉及的数据结构、并发控制和性能优化问题一点都不简单。</p>
                    <p class="text-xl font-medium">每次我看到有人随便用一个for循环遍历几十万条数据，心里就默默为服务器点蜡烛...</p>
                </div>
            </section>
        </article>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-6">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p>探索编程的深度与广度</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300">
                        <i class="fas fa-globe mr-2"></i> www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // Set current date
        const now = new Date();
        const options = { year: 'numeric', month: 'long', day: 'numeric' };
        document.getElementById('current-date').textContent = now.toLocaleDateString('zh-CN', options);
        
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>